Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add optional strict check for printf parameter types #4349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

Copy link
Contributor

@schlndh schlndh commented Sep 19, 2025

Closes phpstan/phpstan#13496
Previous PR: #3977

Would you accept turning this on by default in phpstan-strict-rules?

angeleg reacted with heart emoji
'strict-int' => 'int',
'int' => 'int',
'float' => 'float',
'string' => 'int|float|string|Stringable',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit long and it's not completely accurate either (it accepts classes with __toString() even if they don't implement Stringable). The other alternative I considered was just string, but that might be a bit confusing to users. Is there anything better?

Copy link
Member

@ondrejmirtes ondrejmirtes Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try __stringandstringable. I can see you're already using new StringAlwaysAcceptingObjectWithToStringType in a related place.

schlndh reacted with thumbs up emoji
Copy link
Member

Yes, please contribute seting thing parameter to true in phpstan-strict-rules once this is released 😊

@ondrejmirtes ondrejmirtes merged commit f6ed272 into phpstan:2.1.x Sep 19, 2025
455 of 457 checks passed
Copy link
Member

Thank you!

schlndh reacted with thumbs up emoji

@schlndh schlndh deleted the feature-strictPrintfPlaceholderTypes branch September 20, 2025 05:39
Copy link
Member

Please also send a docs update for phpstan.org about checkStrictPrintfPlaceholderTypes. Thanks!

45,
],
[
'Parameter #2 of function printf is expected to be __stringandstringable by placeholder #1 ("%s"), null given.',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@schlndh @ondrejmirtes After enabling this rule I noticed a lot of errors related to string|null types.

Personally, I think replacing those sprintf call arguments with $value ?? '' feels like making a computer happy. As it does exactly the same as what sprintf does for %s.

See https://3v4l.org/TlHU9#v8.4.13

I know this is a strict rule, but wonder if this was really intentional or something that should be reconsidered?

Copy link
Member

@ondrejmirtes ondrejmirtes Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it's useful that PHPStan lets you know you're potentially passing null to sprintf. You might want to deal with null differently than what sprintf does.

derrabus and VincentLanglet reacted with thumbs up emoji
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally implemented it like that to match my understanding of "strict". However, I encountered the same thing: hundreds of issues like this, most of which I don't care about (though I'm fine with letting them all fall into the baseline). So I would be open to changing it. I'm just not sure what's the best way to go about it.

Strict-rules do allow $null . $string and "{$null}", so I guess this wouldn't have to be as strict either (unless it's allowed by omission rather than intentionally). At the very least it's a bit inconsistent.

On the other hand, there are third-party rules (e.g. shipmonk rules) which do prohibit it, so clearly there is also interest in the fully strict version.

Another thing to consider is whether it should be allowed only in %s or also other placehoders?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strict-rules do allow $null . $string and "{$null}", so I guess this wouldn't have to be as strict either (unless it's allowed by omission rather than intentionally). At the very least it's a bit inconsistent.

This is an excellent point.

Another thing to consider is whether it should be allowed only in %s or also other placehoders?

I would say only for %s.

Copy link
Member

@ondrejmirtes ondrejmirtes Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An idea: string|null should be reported only on level 8+, basicallh RuleLevelHelper should be utilized.

derrabus reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, that won't help me (level 9 🙈)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say only for %s.

To expand on that point: %d + null make 0, which is confusable with, well, passing an actual 0 integer. 😄 So for that reason I wouldn't allow it either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes Re: level 8

  1. report calling methods and accessing properties on nullable types

IMO that's stretching it a little bit. And it wouldn't be consistent with how string + null is handled in other cases (concatenation, interpolation, ...).

Let's consider consistency with phpstan-strict-rules (playground):

  • Strict-rules prohibit bool and null in numerical operations (consistent with %d and %f).
  • Strict-rules allow numeric-string in numerical operations (inconsistent with %f, %d is probably OK since float is excluded as well).
  • Strict-rules prohibit general string in numerical operations (consistent with %d and %f).
  • Strict-rules allow bool and null in string operations (inconsistent with %s).

Idea:

  • Let's enable passing null to %s. If PHPStan later adds a config option to limit type coercions then it could be used here (as well as $string . $null, "{$null}", ...).
  • Let's enable passing numeric-string to %f. I excluded it, because numeric-string could lose precision by doing so. But it's probably too strict.
  • Let's keep passing bool to %s prohibited. I could later try to prohibit bool in $string . $bool and "{$bool}" in strict-rules (no promises).

ruudk, angeleg, and derrabus reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small side note, PHPStorm will tell me to remove (string) $null for %s in a sprintf. It's only fine with $null ?? ''.

Screenshot 2025年09月26日 at 12 42 39@2x

schlndh reacted with thumbs up emoji
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea:

  • Let's enable passing null to %s. If PHPStan later adds a config option to limit type coercions then it could be used here (as well as $string . $null, "{$null}", ...).

I don't know if that helps, but I've reviewed the new errors that popped up in my current project. The dominant error reported was that we pass something that could be null. And in most of the cases, it would be a problem is the value was actually null. It was also old news because we already knew that the code in question is a bit too lax when it comes to handling null. So all errors that the new rule added to our baseline were actually just "more of the same".

  • Let's enable passing numeric-string to %f. I excluded it, because numeric-string could lose precision by doing so. But it's probably too strict.

Yes, it is. In fact, you actually might lose precision when casing a numeric string to a float. And besides it doesn't make much sense to cast a string to a float just to format it as a string again.

schlndh reacted with thumbs up emoji
Copy link

I just stumbled upon this and - what shall I say - the usability seems rather limited. Especially when using the same parameter multiple times in the format string.

https://phpstan.org/r/470b0175-9359-418e-87d5-3a37013bccae

Why does an int not need to be cast to string when the format asks for a string? Why does an int not need to be cast to a float?

I do understand that it can make sense to hint that the formatting parameter will cast a value to something else. But the messages are actually wrong:

For one thing: Parameter #2 of function sprintf is expected to be float by placeholder #3 ("%10ドル.02f") - This is placeholder 1 we are talking about.
And the other thing is that sprintf does not expect the value to be float. It merely will format the value as float. That is a small but important difference! sprinf expects anything. But it will make sure that the passed parameter will be cast to the expected format when integrated into the string according to the rules noted in the format.

And as https://phpstan.org/r/83f7819b-7fef-46dd-a397-c38506bad3b3 and https://phpstan.org/r/ab740815-f7c1-41a8-96ad-2b4d508ca3c9 show it does not apply these rules in every situation. Here I would expect PHPStan to trigger an error whenever something is passed that does not work with the provided type.

In the second example I would also expect PHPStan to understand the function signature that is shown in the PHP documentation ( sprintf(string $format, mixed ...$values): string especially as bool and null are nowhere usable in the formatter-strings, so the values should not be allowed at all - according to the logic that only values are allowed that the formatting string actually expects...

Copy link
Member

@heiglandreas I think it's fine, you don't need to turn on the strict version if you don't want it?

Copy link

I am more concerned about a false sense of "safety" that this rule implies.

Even if I want a very strict type-checking, this does - at least in the current way - not add a reliable safety to the sprintf-function.

  • hard-coded formatting-strings that are passed as a parameter are not taken into account
  • mixed-parameters that require determining the parameter type based on the passed value are not supported
  • argumend-numbering - especially with multiple usages of the same argument - are not taken into account.

So enabling that in the strict-checking by default seems ... an interesting choice. Now one has to explicitly remove this very opinionated and brittle check. Instead of allowing one to opt-in if that is what I want and when I exactly know what to do.

Copy link

So enabling that in the strict-checking by default seems

Strict rules are not enabled by default, are they?

Copy link

If you enable the strict rules, then this rule is also enabled. As I explained above the rule is not really checking in all circumstances and giving a very skewed result.

I can see that it might make sense in certain circumstances. But for sure not in all situations where one wants to enable a strict check.

Copy link
Member

  1. This is in bleeding edge which means things are not set in stone and feedback is very appreciated.
  2. strict-rules are by definition opinionated.

Copy link

Opinionated is fine, lulling into a false sense of security is ... arguable? 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@ondrejmirtes ondrejmirtes ondrejmirtes left review comments

+3 more reviewers

@ruudk ruudk ruudk left review comments

@derrabus derrabus derrabus left review comments

@angeleg angeleg angeleg left review comments

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Validate sprintf argument types against format specifiers

AltStyle によって変換されたページ (->オリジナル) /